/* Andreas Baum
 * ClientThreadPair.java
 * Handles the interaction of the client proxy with the server proxy
 * CS 78 - Networks
 */

/* 	THIS FILE ENDED UP NOT BEING USED, BUT WOULD ALLOW SEVERAL USERS ON THE CLIENT 
 * 	PROXY'S SUBNET TO SEE THE SERVER PROXY
 */

/* 
 * The notion of "server" in this project can lead to confusion: 
 * Indeed, it is employed with two separate meanings.
 * 1. The central login server
 * 2. The "server" side of the iTunes connection to which another proxy connect
 * , and which allows another user to access the user's iTunes library.
 */


package jbpClientConnector;


import java.io.IOException;
import java.net.Socket;
import java.net.UnknownHostException;


public class ClientThreadPair{

	public static int SERVER_PORT = JBPClientConnector.SERVER_PORT;
	public static int ITUNES_PORT = 3689;

	/** Constructor*/
	public ClientThreadPair(Socket serviceSocket, String hostname) throws IOException {
		
		String host = hostname;
		DataThread cExtThread = null;
		// System.out.println("Attempting to connect to " + host + "...");
		try {
			// send connection request:
			Socket extSocket = new Socket(host, JBPClientConnector.SERVER_PORT);
//			 connection is established, hand it over to a client thread.
			cExtThread = new DataThread(extSocket, "cExtThread");
		} catch (UnknownHostException e) {
			// If host can't be found:
			System.err.println("ERROR: Don't know about host: " + host + ".");
			System.exit(1);
		} catch (IOException e) {
			// If host doesn't accept connections:
			System.err.println("ERROR: Couldn't get I/O for "
					+ "the connection to: " + host + ".");
			System.exit(1);
		}

		// Create a new thread for the iTunes service socket:
		DataThread serviceThread = new DataThread(serviceSocket, "iTunesServiceThread");
		// Tell the external thread and the iTunes service thread to pipe things through to each other:
		serviceThread.setOtherThread(cExtThread);
		cExtThread.setOtherThread(serviceThread);

	}
}